package com.three.productList.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.three.core.entity.*;
import com.three.productList.dao.ProductExamineDao;
import com.three.productList.dao.ProductListDao;
import com.three.productList.dao.StoreDao;
import com.three.productList.feign.FrontUserFeign;
import com.three.productList.feign.FrontUserVipFeign;
import com.three.productList.feign.UserMyFeign;
import com.three.productList.service.ProductListService;
import com.three.core.vo.R;
import com.three.productList.vo.*;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


/**
 * @author ：li
 * @date ：Created in 2025/3/11 011 10:26
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class ProductListServiceImpl implements ProductListService {
    @Autowired
    private ProductListDao productListDao;
    @Autowired
    private ProductExamineDao productExamineDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private UserMyFeign userMyFeign;
    @Autowired
    private FrontUserFeign frontUserFeign;
    @Autowired
    private FrontUserVipFeign frontUserVipFeign;
    @Autowired
    private StoreDao storeDao;

    //分页查询商品列表
    @Override
    public R<IPage<ProductVo>> selectProduct(SearchForm searchForm) {
        //获取登陆者的信息
        String token=request.getHeader("token");
        String name = (String) redisTemplate.opsForHash().get("user::login::" + token, "name");
        PanduanVo panduanVo = new PanduanVo();
        panduanVo.setProductName(searchForm.getProductName());
        if(!name.equals("admin")){
            panduanVo.setCreateBy(name);
        }
        Page<Product> page = new Page<>(searchForm.getCurrentPage(),searchForm.getPageSize());
        IPage<ProductVo> products = productListDao.selectMyList(page, panduanVo);
        return new R<IPage<ProductVo>>(200,"查询成功",products);
    }
    //删除商品
    @Override
    public R<Product> deleteByProductId(Integer productId) {
        int i = productListDao.deleteByProductId(productId);
        if(i == 0){
            return new R<Product>(400,"删除失败",null);
        }
        return new R<Product>(200,"删除成功",null);
    }
    //添加商品
    @Override
    public R<Product> insertProduct(Product product) {
        //获取登录者的信息
        String token=request.getHeader("token");
        R<User> userR = userMyFeign.selectByName((String) redisTemplate.opsForHash().get("user::login::"+token,"name"));
        product.setCreateId(userR.getData().getUserId());
        product.setUpdateId(userR.getData().getUserId());

        product.setCreateTime(LocalDateTime.now());
        product.setUpdateTime(LocalDateTime.now());
        int insert = productListDao.insert(product);
        //添加商品审核表
        ProductExamine productExamine=new ProductExamine();
        productExamine.setProductId(product.getProductId());
        productExamine.setStoreId(product.getStoreId());
        productExamine.setProTypeId(product.getProTypeId());
        productExamine.setName(product.getProductName());
        productExamine.setPicture(product.getPicture());
        productExamine.setLoadPrice(product.getLoadPrice());
        productExamine.setNewPrice(product.getNewPrice());
        productExamine.setMonthlySales(product.getMonthlySales());
        productExamine.setIsFreightShipping(product.getIsFreightShipping());
        productExamine.setFreight(product.getFreight());
        productExamine.setIsline(product.getIsline());
        productExamine.setProductDetails(product.getProductDetails());
        productExamine.setPrompt(product.getPrompt());
        productExamine.setExamineId((Integer) redisTemplate.opsForHash().get("user::login::"+token,"id"));
        int insert1 = productExamineDao.insert(productExamine);
        if(insert == 0 || insert1 == 0){
            return new R<Product>(400,"添加失败",null);
        }
        return new R<>(200,"添加成功",null);
    }
    //修改商品，修改审核表
    @Override
    public R<Product> updateProduct(Product product) {
            product.setUpdateTime(LocalDateTime.now());
            int i = productListDao.updateById(product);
            if(i > 0){
                return new R<>(200,"修改成功",null);
            }
        return new R<>(200,"修改成功",null);
    }
    //查询所有商品,添加表单
    @Override
    public R<List<Product>> selectAll() {
        List<Product> list = productListDao.selectMyLists();
        return new R<List<Product>>(200,"查询成功",list);
    }


    //手机端查询商品和店铺名称
    @Override
    public R<List<ProductMobileVo>> selectProductAndStoreNameByProductName(String productName) {
        //根据手机号查询当前的手机用户的id，查询当前用户关注的店铺下的商品
        R<FrontUser> userInfo = frontUserVipFeign.getUserInfo();
        List<ProductMobileVo> productMobileVos = productListDao.selectProductAndStoreNameByProductName(productName, userInfo.getData().getUserId());
        return new R<List<ProductMobileVo>>(200,"查询成功",productMobileVos);
    }
    //手机端查询商品
    @Override
    public R<List<Product>> selectProductImagesByProductId(Integer productId) {
        List<Product> products = productListDao.selectProductImagesByProductId(productId);
        return new R<>(200,"查询成功",products);
    }
    //手机端根据类型和名字查询商品
    @Override
    public R<List<ProductAndStoreNameVo>> selectByType(Integer proTypeId, String productName) {
        List<ProductAndStoreNameVo> products = productListDao.selectMyMobileList(proTypeId,productName);
        return new R<>(200,"查询成功",products);
    }
    // 手机端根据店铺id和商品名查询商品
    @Override
    public R<List<ProductMobileVo>> selectProductAndStoreNameByStoreId(String productName, Integer storeId) {
        List<ProductMobileVo> productMobileVos = productListDao.selectProductAndStoreNameByStoreId(productName, storeId);
        return new R<>(200,"查询成功",productMobileVos);
    }

    @Override
    public R ppraise(ProductAppraiseVo productAppraiseVo) {
        int appraise = productListDao.appraise(productAppraiseVo);
        if(appraise == 1){
            return new R(200,"评价成功",null);
        }
        return new R(400,"评价失败",null);
    }
    //查询当前登录者的商店
    @Override
    public R<List<Store>> selectStoreByUserId() {
        //获取当前登录者的信息
//        String token=request.getHeader("token");
//        R<User> userR = userMyFeign.selectByName((String) redisTemplate.opsForHash().get("user::login::"+token,"name"));
//        Store store = storeDao.selectById(userR.getData().getUserId());
        List<Store> store = storeDao.selectStoreList();
        return new R<List<Store>>(200,"查询成功",store);
    }


}
